# 二维
def iouTwoDim(box1, box2):
    # box的前两个数表示垂直方向，后两个数表示水平方向
    up = max(box1[0], box2[0])
    down = min(box1[1], box2[1])
    if(down < up):
        interV = 0
    else:
        interV = down - up

    left = max(box1[2], box2[2])
    right = min(box1[3], box2[3])
    if right < left:
        interH = 0
    else:
        interH = right - left

    inter = interH * interV
    union = (box1[1] - box1[0]) * (box1[3] - box1[2]) + (box2[1] - box2[0]) * (box2[3] - box2[2]) - inter

    iou = inter / union

    return iou

box1 = [1, 9, 3, 15]
box2 = [2, 15, 7, 20]
iou  = iouTwoDim(box1, box2)
print(iou)